﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace WeChatDataSrc
{
    /// <summary>
    /// 实况
    /// </summary>
    class SceneData
    {
        static string URL = ConfigurationSettings.AppSettings["SceneURL"];
        static string StationCodeString = ConfigurationSettings.AppSettings["StationCode"];
        
        public static void UploadData(DateTime? sd)
        {
            DateTime NowMaxDateTime;
            if (DateTime.TryParse(ReadMax.ReadMaxScene(), out NowMaxDateTime) == false)
            {
                NowMaxDateTime = DateTime.Now;
            }
            else
            {
                NowMaxDateTime = NowMaxDateTime.AddMinutes(10);
            }

            if (sd.HasValue)
            {
                NowMaxDateTime = Convert.ToDateTime(sd);
            }
            string NowYearMonth = NowMaxDateTime.ToString("yyyyMM");
            DateTime Now = DateTime.Now;
            
            while (true)
            {
                StringBuilder sb = new StringBuilder(1024 * 15);
                string Result = string.Empty;
                if (NowMaxDateTime.CompareTo(Now) > 0) break;
                sb.Append(NowMaxDateTime.ToString("yyyy-MM-dd HH:mm") + "^");
                string sql = string.Format("select Station_Num,Data_time,Temperature,Temperature_max,Temperature_min,WindDirection_2M_ave,WindSpeed_2M_ave,Rain_1hCur from JSZDZ_Month{0} where data_time='{1}'", NowMaxDateTime.ToString("yyyyMM"), NowMaxDateTime.ToString("yyyyMMddHHmm"));
                DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.Conn_String, CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    SceneDataModel SceneModel;
                    foreach (DataRow row in dt.Rows)
                    {
                        var tmp = string.Format("{0}:nowt*{1}|maxt*{2}|mint*{3}|wind_direction*{4}|wind_speed*{5}|rain1h*{6}^",
                            row[0].ToString(),
                            row[2].ToString(),
                            row[3].ToString(),
                            row[4].ToString(),
                            row[5].ToString(),
                            row[6].ToString(),
                            row[7].ToString()
                            );

                        sb.Append(tmp);
                    }
                }
                if (sb.ToString().Length > 17)
                {
                    HttpPost.SendData(URL, "data=" + sb.ToString());
                }
                NowMaxDateTime = NowMaxDateTime.AddMinutes(10);
            }
        }

        private class SceneDataModel
        {
            public string datetime { get; set; }
            public string station { get; set; }
            public string maxt { get; set; }
            public string mint { get; set; }
            public string nowt { get; set; }
            public string wind_speed { get; set; }
            public string wind_direction { get; set; }
            public string rain1h { get; set; }
        }
    }
    
}
